package org.zuel.boot.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.zuel.boot.demo.web.*;
import org.zuel.boot.demo.exception.ParameterException;
import org.zuel.boot.demo.entity.Company;
import org.zuel.boot.demo.mapper.CompanyMapper;
import org.zuel.boot.demo.service.ICompanyService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


/**
 *  服务实现类
 *
 * @author ikun
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {

    @Override
    public IPage<Company> list(PageParam<Company> pageParam) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        if (pageParam.getPageData() != null) {
            if (pageParam.getPageData().get("name") != null) {
                queryWrapper.like("name", pageParam.getPageData().get("name"));
            }
            if (pageParam.getPageData().get("nature") != null) {
                queryWrapper.like("nature", pageParam.getPageData().get("nature"));
            }
            if (pageParam.getPageData().get("industry") != null) {
                queryWrapper.like("industry", pageParam.getPageData().get("industry"));
            }
            if (pageParam.getPageData().get("province") != null) {
                queryWrapper.like("province", pageParam.getPageData().get("province"));
            }
            if (pageParam.getPageData().get("city") != null) {
                queryWrapper.like("city", pageParam.getPageData().get("city"));
            }
        }
        Page<Company> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        return this.page(page, queryWrapper);
    }

    @Override
    public JsonResult read(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }

        Company company = baseMapper.selectById(id);
        return JsonResult.ok().setData(company);
    }

    @Override
    public JsonResult add(Company company) {
        if (company ==null){
            throw new ParameterException("参数对象为空");
        }

        baseMapper.insert(company);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult update(Company company) {
        if (company == null) {
            throw new ParameterException("参数对象为空");
        }

        if (company.getId() == null) {
            throw new ParameterException("参数对象id为空");
        }

        baseMapper.updateById(company);
        return JsonResult.ok("修改成功");
    }

    @Override
    public JsonResult delete(Integer[] ids) {
        if (ids == null) {
            throw new ParameterException("参数对象为空");
        }

        if (ids != null || ids.length > 0) {
            baseMapper.deleteBatchIds(Arrays.asList(ids));
        }
        return JsonResult.ok("删除成功");
    }

}